package com.chima.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.Department;
import com.chima.domain.Jobstyle;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.service.DepartmentService;
import com.chima.service.JobstyleService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


import flexjson.JSONSerializer;
public class JobstyleAction extends ActionSupport implements ModelDriven<Jobstyle>{
	
	Jobstyle jobstyle=new Jobstyle();
	public Jobstyle getModel() {
		return jobstyle;
	}
	
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private boolean success=false ;
	private JobstyleService jobstyleService;
	private List<Jobstyle> jobstyles;
	public JobstyleService getJobstyleService() {
		return jobstyleService;
	}

	public void setJobstyleService(JobstyleService jobstyleService) {
		this.jobstyleService = jobstyleService;
	}

	public List<Jobstyle> getJobstyles() {
		return jobstyles;
	}

	public void setJobstyles(List<Jobstyle> jobstyles) {
		this.jobstyles = jobstyles;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String  listJobstyle() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean = jobstyleService.findJobstyle(pageRequestBean);
		jobstyles=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.serialize(jobstyles);  
        String resultJson2 =jsonSerializer.serialize(total);
        System.out.println(resultJson2);
        String jsonString = "{total:"+resultJson2.toString()+",jobstyles:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	public void addJobstyle() throws Exception{
		try {
			jobstyleService.saveJobstyle(jobstyle);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	
	public void editJobstyle() throws Exception{
	jobstyleService.updateJobstyle(jobstyle);
	this.success=true;
	JSONSerializer jsonSerializer = new JSONSerializer();
	String resultJson =jsonSerializer.serialize(success);
	String jsonString = "{success:"+resultJson.toString()+"}";
	System.out.println(jsonString);
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
}
public String deleteJobstyle(){
		jobstyleService.deleteJobstyle(jobstyle.getId());
	return NONE;
}



}
